package app.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import base.exception.ExceptionCode;
import app.model.sys.SysUser;
import app.util.LoginUserUtil;

@Service
public class LoginInterceptor extends HandlerInterceptorAdapter{

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		// TODO Auto-generated method stub
		
		SysUser loginUser = (SysUser)request.getSession().getAttribute("sysUser");
		if(loginUser==null){
//			request.getRequestDispatcher("/login.jsp");  
//			request.getRequestDispatcher("/upload.html").forward(request, response);
//			response.sendRedirect("/upload.html");
//			request.setAttribute("status", 999);
			response.setStatus(ExceptionCode.NO_LOGIN);
			return false;
		}
//		

		LoginUserUtil.loginUser = loginUser;
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}

}
